/*
 * BanBu_ApplyVipViewController
 * @description 本文件提供开通vip会员界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供开通vip会员界面的ViewController
 */

#import "BanBu_ApplyVipViewController.h"
#import "BanBu_VideoAuthenViewController.h"
#import "AppDataManager.h"
#import "BanBu_AppDelegate.h"
#import "AppCommunicationManager.h"
#import "BanBu_VIPAlert.h"
@interface BanBu_ApplyVipViewController ()
{
//    CGFloat viewHeight;
    
    BOOL videoAuthBOOL;
    BOOL avatarPassBOOL;
    BOOL locationPassBOOL;
    BOOL pushidPassBOOL;
    BOOL regPassBOOL;
}
@end

@implementation BanBu_ApplyVipViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)checkCanApplyVIP
{
    if(videoAuthBOOL&&avatarPassBOOL&&locationPassBOOL&&pushidPassBOOL&&regPassBOOL)
    {
        [VIPApplyButton setBackgroundImage:[UIImage imageNamed:@"startverify.png"] forState:UIControlStateNormal];
        VIPApplyButton.userInteractionEnabled = YES;
    }
    else
    {
        [VIPApplyButton setBackgroundImage:[UIImage imageNamed:@"sexSelectBV.png"] forState:UIControlStateNormal];
        VIPApplyButton.userInteractionEnabled = NO;
    }
}
-(IBAction)applyVIP:(id)sender
{
    if(videoAuthBOOL&&avatarPassBOOL&&locationPassBOOL&&pushidPassBOOL&&regPassBOOL)
    {
        [AppComManager getBanBuData:BanBu_Set_Vipuser_Request_Byuser par:[NSDictionary dictionary] delegate:self];
        
    }
    else
    {
        
    }
}
-(void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"applyVipTitle", nil);
    videoPassLabel.text = NSLocalizedString(@"videopass", nil);
    avatarPassLabel.text = NSLocalizedString(@"avatarPass", nil);
    regPassLabel.text = NSLocalizedString(@"regPass", nil);
    locationPassLabel.text = NSLocalizedString(@"locationPass", nil);
    pushidPassLabel.text = NSLocalizedString(@"pushidPass", nil);

    vipConditionTitle.text = NSLocalizedString(@"upgradeVIPConditionTitle", nil);
    vipCondition.text = NSLocalizedString(@"pgradeVIPCondition", nil);
    vipRightTitle.text = NSLocalizedString(@"VIPRightListTitle", nil);
    vipRight.text = NSLocalizedString(@"VIPRightList", nil);
    
    [VIPApplyButton setTitle:NSLocalizedString(@"applyVipTitle", nil) forState:UIControlStateNormal];

//    videoPass;
//    avatarPass;
//    regPass;
//    locationPass;
//    pushidPass;
    
}
-(void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    //顶部vip已经完成情况的五行的frame
    
    CGFloat viewHeight = 0;
    viewHeight = 60;
    vipApplyView.frame = CGRectMake(0, viewHeight, 320, viewHeight+50*5);
    viewHeight += 50*5;
//    viewHeight += 10;
    
    //vip 条件和权利的高度
    CGFloat height = 30;
    vipConditionTitle.frame = CGRectMake(20, height, 280, 40);
    height += (40+10);
    
    
    CGFloat textHeight = [NSLocalizedString(@"pgradeVIPCondition", nil) sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:kTextLineBreakByWordWrapping].height;
    vipCondition.frame = CGRectMake(20,height, 280, textHeight+20);
    height += (textHeight+20+10);
    
    vipRightTitle.frame = CGRectMake(20, height, 280, 40);
    height += (40+10);
    
    textHeight = [NSLocalizedString(@"VIPRightList", nil) sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:kTextLineBreakByWordWrapping].height;
    vipRight.frame = CGRectMake(20, height, 280, textHeight+20);
    height += (textHeight+20+10);
    

    //底部容器view的frame
    
    vipConditionRightView.frame = CGRectMake(0, viewHeight, 320, height);
    viewHeight += height;
    
    scrollView.contentSize = CGSizeMake(320, viewHeight);
    
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIImage *duihao = [UIImage imageNamed:@"duihao.png"];
    UIImage *dacha  = [UIImage imageNamed:@"dacha.png"];
    videoAuthBOOL = [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"verify"] boolValue];
    avatarPassBOOL = [[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"verify"] boolValue];
    locationPassBOOL = MyAppDataManager.locationServicesEnabled;
    
    if(![[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"verify"] boolValue])
    {
        [videoPass setImage:dacha forState:UIControlStateNormal];
        [avatarPass setImage:dacha forState:UIControlStateNormal];
        _videoPassImage.hidden = NO;
        _avatarPassImage.hidden = NO;
    }
    else
    {
        _videoPassImage.hidden = YES;
        _avatarPassImage.hidden = YES;
        [videoPass setImage:duihao forState:UIControlStateNormal];
        [avatarPass setImage:duihao forState:UIControlStateNormal];
    }
    
    if(MyAppDataManager.locationServicesEnabled)
    {
        _locationPassImage.hidden = YES;
        [locationPass setImage:duihao forState:UIControlStateNormal];
    }
    else
    {
        _locationPassImage.hidden = NO;
        [locationPass setImage:dacha forState:UIControlStateNormal];
    }
    BanBu_AppDelegate *ap = (BanBu_AppDelegate *)[UIApplication sharedApplication].delegate;
    if(ap.pushid&&ap.pushid.length)
    {
        _pushidPassImage.hidden = YES;
        pushidPassBOOL = YES;
        [pushidPass setImage:duihao forState:UIControlStateNormal];
        
    }
    else
    {
        _pushidPassImage.hidden = NO;
        pushidPassBOOL = NO;
        [pushidPass setImage:dacha forState:UIControlStateNormal];
    }
    NSDateFormatter * formatter = [[[NSDateFormatter alloc]init] autorelease];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *regDate = [formatter dateFromString:[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"regdate"]];
    NSInteger timeInterval = [[NSDate date] timeIntervalSinceDate:regDate];
    
//    NSLog(@"%@ %@ regDate : %@ timeInterval : %d",[[UserDefaults valueForKey:MyAppDataManager.useruid] valueForKey:@"regdate"],[NSDate date],regDate,timeInterval);
    if(timeInterval >= 3*24*3600)
    {
        regPassBOOL = YES;
        _regPassImage.hidden = YES;
        [regPass setImage:duihao forState:UIControlStateNormal];
    }
    else
    {
        _regPassImage.hidden = NO;
        regPassBOOL = NO;
        [regPass setImage:dacha forState:UIControlStateNormal];
    }
    [self checkCanApplyVIP];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)startApplyVip:(UIButton *)sender
{
    switch (sender.tag) {
        case 100:
        {
            if(videoAuthBOOL)
            {
                return;
            }

            [[BanBu_VIPAlert shareInstance] GoVerifyApplyViewAlertFor:self title:NSLocalizedString(@"videoVerify", nil) message:NSLocalizedString(@"verifyNoticeAlert", nil)];
        }
            break;
        case 101:
        {
            if(avatarPassBOOL)
            {
                return;
            }
            BanBu_VideoAuthenViewController *verify = [[BanBu_VideoAuthenViewController alloc] initWithRegisterVerify:NO];
            [self.navigationController pushViewController:verify animated:YES];
            [verify release];
        }
            break;
        case 102:
        {
        }
            break;
        case 103:
        {
            if(locationPassBOOL)
            {
                return;
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"noticeNotice", nil) message:NSLocalizedString(@"openLocationService", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
            break;
        case 104:
        {
            if(pushidPassBOOL)
            {
                return;
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"noticeNotice", nil) message:NSLocalizedString(@"openPushNotificationService", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"取消", nil) otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
            break;
            
        default:
            break;
    }
    
}
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
   if(error)
   {
       [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"applyVipFail", nil) activityAnimated:NO duration:1];
   }
    else
    {
        if([AppComManager respondsDic:resDic isFunctionData:BanBu_Set_Vipuser_Request_Byuser])
        {
            if([resDic valueForKey:@"ok"])
            {
                [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"applyVipSucceed", nil) activityAnimated:NO duration:1];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"applyVipFail", nil) activityAnimated:NO duration:1];
            }
        }
    }
}


- (void)dealloc {
    [_videoPassImage release];
    [_avatarPassImage release];
    [_pushidPassImage release];
    [_locationPassImage release];
    [_regPassImage release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setVideoPassImage:nil];
    [self setAvatarPassImage:nil];
    [self setPushidPassImage:nil];
    [self setLocationPassImage:nil];
    [self setRegPassImage:nil];
    [super viewDidUnload];
}
@end
